<?php
declare (strict_types = 1);

namespace app\model\communal;

use app\facade\StringFacade;
use app\model\concern\AmisTrait;
use think\Model;
use think\db\Query;
use think\model\relation\BelongsTo;
use think\model\concern\SoftDelete;

class ScaleMultiModel extends Model
{
    use AmisTrait;

    /**
     * 数据库配置
     *
     * @var string
     */
    protected $connection = 'communal';

    /**
     * 主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 表名
     *
     * @var string
     */
    protected $table = '70100_scale_multi';

    /**
     * 自动时间戳
     *
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间字段设置
     *
     * @var string
     */
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    /**
     * 软删除
     */
    use SoftDelete;
    protected $deleteTime = 'delete_at';
    protected $defaultSoftDelete = 0;

    /**
     * 展示字段名
     *
     * @var string[]
     */
    public static $column = [
        'id', 'admin_id', 'image', 'name', 'intro', 'scale_category_id', 'amount', 'questions_number', 'evaluation_number',
        'report_number', 'content', 'is_recommend', 'is_featured', 'sales_number', 'state', 'created_at'
    ];

    /**
     * 搜索字段名
     *
     * @var string[]
     */
    public static $searchColumn = [
        'id', 'image', 'name', 'scale_category_id', 'amount', 'questions_number', 'evaluation_number',
        'report_number', 'content', 'is_recommend', 'is_featured', 'state', 'createtime',

        /*
         * app
         */
        'price_option',

        /*
         * 多选
         */
        'scale_category_ids',
    ];

    /**
     * 新增字段
     *
     * @var string[]
     */
    public static $saveColumn = [
        'image', 'name', 'intro', 'scale_category_id', 'amount', 'questions_number', 'evaluation_number',
        'report_number', 'content', 'is_recommend', 'is_featured', 'state'
    ];

    /**
     * 更新字段
     *
     * @var string[]
     */
    public static $updateColumn = [
        'id', 'image', 'name', 'intro', 'scale_category_id', 'amount', 'questions_number', 'evaluation_number',
        'report_number', 'content', 'is_recommend', 'is_featured',
    ];

    /**
     * @var string[]
     */
    public static $itemColumn = ['id', 'name', 'state'];

    /**
     * 测试价格选项
     */
    const PRICE_OPTION_ZERO = 0; // 全部
    const PRICE_OPTION_ONE = 1; // 免费
    const PRICE_OPTION_TWO = 2; // 付费

    /**
     * 测试价格选项 Options
     */
    public static $priceOptions = [
        ['id' => self::PRICE_OPTION_ZERO, 'name' => '全部'],
        ['id' => self::PRICE_OPTION_ONE, 'name' => '免费'],
        ['id' => self::PRICE_OPTION_TWO, 'name' => '付费'],
    ];

    /**
     * 排序选项
     */
    const ORDER_OPTION_ONE = 1; // 综合排序
    const ORDER_OPTION_TWO = 2; // 低价优先
    const ORDER_OPTION_THREE = 3; // 销量优先

    /**
     * 排序选项 Options
     */
    public static $orderOptions = [
        ['id' => self::ORDER_OPTION_ONE, 'name' => '综合排序'],
        ['id' => self::ORDER_OPTION_TWO, 'name' => '低价优先'],
        ['id' => self::ORDER_OPTION_THREE, 'name' => '销量优先'],
    ];

    /**
     * 排序规则映射
     *
     * @var string[]
     */
    public static $orderRuleMap = [
        1 => ['orderBy' => 'created_at', 'orderDir' => 'desc'],
        2 => ['orderBy' => 'amount', 'orderDir' => 'asc'],
        3 => ['orderBy' => 'sales_number', 'orderDir' => 'desc'],
    ];

    /**
     * 修改
     *
     * @param $value
     * @return string|string[]
     */
    public function setImageAttr($value)
    {
        return StringFacade::replaceDomainShowUrl($value);
    }

    /**
     * Image 获取
     *
     * @param $value
     * @param $data
     * @return mixed|string
     */
    public function getImageAttr($value, $data)
    {
        if (!empty($value)) {
            $value = StringFacade::splicingDomainShowUrl($value);
        }

        return $value;
    }

    /**
     * 修改
     *
     * @param $value
     * @return string|string[]
     */
    public function setContentAttr($value)
    {
        return StringFacade::replaceDomainShowUrl($value);
    }

    /**
     * Image 获取
     *
     * @param $value
     * @param $data
     * @return mixed|string
     */
    public function getContentAttr($value, $data)
    {
        if (!empty($value)) {
            $value = StringFacade::splicingDomainShowUrl($value);
        }

        return $value;
    }

    /**
     * scale_category_ids 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchScaleCategoryIdsAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $value = explode(',', $value);
            $query->whereIn('scale_category_id', $value);
        }
    }

    /**
     * scale_category_id 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchScaleCategoryIdAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $query->where('scale_category_id', $value);
        }
    }

    /**
     * name 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchNameAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $query->where('name', 'like', '%' . $value . '%');
        }
    }

    /**
     * price_option 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchPriceOptionAttr(Query $query, $value)
    {
        switch ($value) {
            // 免费
            case self::PRICE_OPTION_ONE:
                $query->where('amount', '<=', 0);
                break;
            case self::PRICE_OPTION_TWO:
                $query->where('amount', '>', 0);
                break;
        }
    }

    /**
     * 关联创建人
     *
     * @return BelongsTo
     */
    public function belongCategory()
    {
        return $this->belongsTo(ScaleCategoryModel::class, 'scale_category_id', 'id')
            ->removeOption('soft_delete')
            ->bind(['category_name' => 'name']);
    }
}
